package com.gxa.controller;

import com.gxa.BusinessException;
import com.gxa.pojo.OutStorage;
import com.gxa.service.OutStorageService;
import com.gxa.utils.JsonResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * @author 陈静
 * @date 2020/9/25 14:37
 */
@Controller
@RequestMapping("/outStorage")
@CrossOrigin
public class OutStorageController {
    @Resource
    OutStorageService outStorageService;

    @ResponseBody
    @RequestMapping(value = "/findStoreName",method = RequestMethod.POST)
    /**
     * 查询所有仓库名字
     * @return 供仓库名字
     */
    public Map<String, Object> findStoreName() throws BusinessException {
        List<Map<String, Object>> storeName = outStorageService.findStoreName();
        JsonResult jsonResult = new JsonResult();
        if(storeName != null) {
            jsonResult.setCode("200");
            jsonResult.setData(storeName);
            jsonResult.setMsg("success");
        }else{
            throw new BusinessException("系统异常，请稍后再试！");
        }
        return jsonResult.getValues();
    }

    /**
     * 根据时间或者供货厂商或者进货仓库或者审核状态查询
     * @param map
     * @return 入库信息
     */
    @ResponseBody
    @RequestMapping(value = "/findOutStorage",method = RequestMethod.POST)
    public Map<String,Object> findInStorage(@RequestBody(required = false) Map<String,Object> map)  {
        JsonResult jsonResult = new JsonResult();
        String  number= map.get("number").toString();
        String size= map.get("size").toString();
        Integer offset =(Integer.parseInt(number)-1)*Integer.parseInt(size);
        Integer limit=Integer.parseInt(size);
        map.put("offset",offset);
        map.put("limit",limit);
        String startDate = map.get("startDate").toString();
        String endDate = map.get("endDate").toString();
        if ( (startDate!= null && !"".equals(startDate))  && (endDate!= null && !"".equals(endDate))) {
            Timestamp startDate2 = Timestamp.valueOf(startDate);
            Timestamp endDate2 = Timestamp.valueOf(endDate);
            map.put("startDate2", startDate2);
            map.put("endDate2", endDate2);
            List<OutStorage> outStorage = outStorageService.findOutStorage(map);
            Integer integer = outStorageService.countFindOutStorage(map);
            jsonResult.setData(outStorage);
            jsonResult.put("count", integer);
            jsonResult.setCode("200");
            jsonResult.setMsg("success");
        }else {
            List<OutStorage> outStorage = outStorageService.findOutStorage(map);
            Integer integer = outStorageService.countFindOutStorage(map);
            jsonResult.setData(outStorage);
            jsonResult.put("count", integer);
            jsonResult.setCode("200");
            jsonResult.setMsg("success");
        }

        return jsonResult.getValues();
    }
    // number 页码    size：每页显示记录数
    /**
     *
     * @param map 起始页 每一页大小
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/outStoragePage",method = RequestMethod.POST)
    public Map findAllInStorageByPage(@RequestBody Map map){
        String  number= map.get("number").toString();
        String size= map.get("size").toString();
        Integer offset =(Integer.parseInt(number)-1)*Integer.parseInt(size);
        Integer limit=Integer.parseInt(size);
        List<OutStorage> outStorages = outStorageService.findAllOutStorageByPage(offset, limit);
        Integer count = outStorageService.findAllOutStorage(); // select count(*) from
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(outStorages);
        jsonResult.put("count",count);
        return jsonResult.getValues();
    }

    /**
     *  修改审核状态
     * @param map id
     * @return  返回影响行数
     */
    @ResponseBody
    @RequestMapping(value = "/updateApproval",method = RequestMethod.POST)
    public Map updateApproval(@RequestBody Map map) throws BusinessException {
        Integer line = outStorageService.updateState(map);
        JsonResult jsonResult = new JsonResult();
        if(line >0){
            jsonResult.setCode("200");
            jsonResult.setMsg("success");
        }else {
            throw new BusinessException("系统异常，请稍后再试！");
        }
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping(value = "/findCustomerName",method = RequestMethod.POST)
    /**
     * 查询所有客户名称
     * @return 客户名称
     */
    public Map<String, Object> findCustomerName() throws BusinessException {
        List<Map<String, Object>> customerName = outStorageService.findCustomerName();
        JsonResult jsonResult = new JsonResult();
        if(customerName != null) {
            jsonResult.setCode("200");
            jsonResult.setData(customerName);
            jsonResult.setMsg("success");
        }else{
            throw new BusinessException("系统异常，请稍后再试！");
        }
        return jsonResult.getValues();
    }

    /**
     *  查询所有品牌名
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findBrandName",method = RequestMethod.POST)
    public Map<String,Object> findBrandName() throws BusinessException {
        List<Map<String, Object>> brandName = outStorageService.findBrandName();
        JsonResult jsonResult = new JsonResult();
        if(brandName != null) {
            jsonResult.setCode("200");
            jsonResult.setData(brandName);
            jsonResult.setMsg("success");
        }else{
            throw new BusinessException("系统异常，请稍后再试！");
        }
        return jsonResult.getValues();
    }

    /**
     *  查询所有分类名称
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findLevelName",method = RequestMethod.POST)
    public Map<String,Object> findLevelName() throws BusinessException {
        List<Map<String, Object>> levelName = outStorageService.findLevelName();
        JsonResult jsonResult = new JsonResult();
        if(levelName != null) {
            jsonResult.setCode("200");
            jsonResult.setData(levelName);
            jsonResult.setMsg("success");
        }else{
            throw new BusinessException("系统异常，请稍后再试！");
        }
        return jsonResult.getValues();
    }

    /**
     * 根据品牌和分类查询出库商品信息
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findOutStorageGoods",method = RequestMethod.POST)
    public Map<String,Object> findOutStorageGoods(@RequestBody(required = false) Map<String,Object> map)  {
        String  number= map.get("number").toString();
        String size= map.get("size").toString();
        Integer offset =(Integer.parseInt(number)-1)*Integer.parseInt(size);
        Integer limit=Integer.parseInt(size);
        map.put("offset",offset);
        map.put("limit",limit);
        List<Map<String, Object>> outStorageGoods = outStorageService.findOutStorageGoods(map);
        Integer integer = outStorageService.countFindOutStorageGoods(map);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(outStorageGoods);
        jsonResult.setCode("200");
        jsonResult.setMsg("success");
        jsonResult.put("count",integer);
        return jsonResult.getValues();
    }
    /**
     *  查询对应的出库信息
     * @param map id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findOutStorageById",method = RequestMethod.POST)
    public Map findOutStorageById(@RequestBody Map map){
        OutStorage outStorage = outStorageService.findOutStorageById(Integer.parseInt(map.get("id").toString()));
        List<Map<String, Object>> outStorageGoods = outStorageService.findOutStorageGoodsByOutStorageId(Integer.parseInt(map.get("id").toString()));
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.put("outStorage",outStorage);
        jsonResult.put("outStorageGoods",outStorageGoods);
        jsonResult.setMsg("success");
        return jsonResult.getValues();
    }

    /**
     * 删除对应的出库信息
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteOutStorageById",method = RequestMethod.POST)
    public Map deleteOutStorageById(@RequestBody Map map) throws BusinessException {
        Integer goodsLines = outStorageService.deleteOutStorageGoodsByOutStorageId(Integer.parseInt(map.get("id").toString()));
        Integer inStorageLines = outStorageService.deleteOutStorageById(Integer.parseInt(map.get("id").toString()));
        JsonResult jsonResult = new JsonResult();
        if( goodsLines<0 && inStorageLines<0){
            throw new BusinessException("系统异常，请稍后再试！");
        }else {
            jsonResult.setCode("200");
            jsonResult.setMsg("success");
        }
        return jsonResult.getValues();
    }

    /**
     *  插入出库信息和商品信息
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/insertOutStorage",method =RequestMethod.POST )
    public Map insertInStorage(@RequestBody Map<String,Object> map) throws BusinessException {
        Integer integer = outStorageService.insertOutStorage(map);
        JsonResult jsonResult = new JsonResult();
        if(integer>-1) {
            jsonResult.setCode("200");
            jsonResult.setMsg("success");
        }else{
            throw new BusinessException("系统异常，请稍后再试！");
        }
        return jsonResult.getValues();
    }

    /**
     * 修改出库信息
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateOutStorage",method = RequestMethod.POST)
    public Map updateOutStorage(@RequestBody Map map) throws BusinessException {
        Integer integer = outStorageService.updateOutStorage(map);
        JsonResult jsonResult = new JsonResult();
        if(integer>-1){
            jsonResult.setCode("200");
            jsonResult.setMsg("success");
        }else{
            throw new BusinessException("系统异常，请稍后再试！");
        }
        return jsonResult.getValues();
    }

}
